﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;

namespace server.Model
{
    class SQLiteDatabase
    {
        private string dbConnection;
        private SQLiteConnection cnn;

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SQLiteDatabase()
        {
            dbConnection = "Data Source=VanetRID.db";
            cnn = new SQLiteConnection(dbConnection);
        }

        /// <summary>
        /// 表查询
        /// </summary>
        /// <param name="sql_query"></param>
        /// <returns>表</returns>
        public DataTable GetDataTable(string sql_query)
        {
            DataTable dt = new DataTable();
            try
            {
                cnn.Open();
                SQLiteCommand sql_com = new SQLiteCommand(cnn);
                sql_com.CommandText = sql_query;
                SQLiteDataReader sql_read = sql_com.ExecuteReader();
                dt.Load(sql_read);
                sql_read.Close();
                cnn.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return dt;
        }

        public DataTable GetDataTable(string sql_query, IList<SQLiteParameter> cmdparams)
        {
            DataTable dt = new DataTable();
            try
            {
                cnn.Open();
                SQLiteCommand sql_com = new SQLiteCommand(cnn);
                sql_com.CommandText = sql_query;
                sql_com.Parameters.AddRange(cmdparams.ToArray());
                SQLiteDataReader sql_read = sql_com.ExecuteReader();
                dt.Load(sql_read);
                sql_read.Close();
                cnn.Close();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return dt;
        }

        /// <summary>
        /// query语句执行
        /// </summary>
        /// <param name="sql_query"></param>
        /// <returns>是否执行成功</returns>
        public bool ExecuteNonQuery(string sql_query)
        {
            bool sucstate = false;
            cnn.Open();
            using (SQLiteTransaction sql_trans = cnn.BeginTransaction())
            {
                SQLiteCommand sql_com = new SQLiteCommand(sql_query, cnn);
                try
                {
                    sql_com.ExecuteNonQuery();
                    sql_trans.Commit();
                    sucstate = true;
                    cnn.Close();
                }
                catch (Exception e)
                {
                    sql_trans.Rollback();
                    throw new Exception(e.Message);
                }
                finally
                {
                    sql_com.Dispose();
                    cnn.Close();
                }
            }
            return sucstate;
        }

        public bool ExecuteNonQuery(string sql_query, IList<SQLiteParameter> cmdparams)
        {
            bool sucstate = false;
            cnn.Open();
            using (SQLiteTransaction sql_trans = cnn.BeginTransaction())
            {
                SQLiteCommand sql_com = new SQLiteCommand(sql_query, cnn);
                try
                {
                    sql_com.Parameters.AddRange(cmdparams.ToArray());
                    sql_com.ExecuteNonQuery();
                    sql_trans.Commit();
                    sucstate = true;
                    cnn.Close();
                }
                catch (Exception e)
                {
                    sql_trans.Rollback();
                    throw new Exception(e.Message);
                }
                finally
                {
                    sql_com.Dispose();
                    cnn.Close();
                }
            }
            return sucstate;
        }
    }
}
